package day02;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import util.JdbcUtil;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet("/showModify")
public class ShowModifyEmpServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("ShowModifyEmpServlet.service");

        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        PrintWriter out=response.getWriter();

        Integer id=Integer.parseInt(request.getParameter("id"));

        System.out.println("id = " + id);

        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try{
            con= JdbcUtil.getConnection();
            con.setAutoCommit(false);


            String sql=new StringBuffer()
                    .append("select * ")
                    .append("from t_emp ")
                    .append("where id=? ")
                    .toString();

            ps=con.prepareStatement(sql);

            ps.setInt(1,id);

            rs=ps.executeQuery();

            if(rs.next()){
                String name=rs.getString("name");
                Double salary=rs.getDouble("salary");
                String sex=rs.getString("sex");
                String edu=rs.getString("edu");
                String hobbies=rs.getString("hobbies");
                String description=rs.getString("description");



                out.println("<!DOCTYPE html>");
                out.println("<html lang=\"en\">");
                out.println("<head>");
                out.println("    <meta charset=\"UTF-8\">");
                out.println("    <title>Title</title>");
                out.println("</head>");
                out.println("<body>");
                out.println("    <form action=\""+request.getContextPath()+"/modify\" method=\"post\">");
                out.println("      <input type=\"hidden\" name=\"id\" value=\""+id+"\"/>");
                out.println("      姓名：<input type=\"text\" name=\"name\" value=\""+name+"\"/><br/>");
                out.println("      工资：<input type=\"password\" name=\"salary\" value=\""+salary+"\"/><br/>");
                out.println("      性别：<input type=\"radio\" name=\"sex\" value=\"1\" "+("1".equals(sex)? "checked=\"checked\"" : "")+"/>男");
                out.println("           <input type=\"radio\" name=\"sex\" value=\"0\" "+("0".equals(sex)? "checked=\"checked\"" : "")+"/>女<br/>");
                out.println("      学历：<select name=\"edu\">");
                out.println("              <option value=\"-1\">---请选择---</option>");
                out.println("              <option value=\"1\" "+("1".equals(edu)? "selected=\"selected\"" : "")+">高中</option>");
                out.println("              <option value=\"2\" "+("2".equals(edu)? "selected=\"selected\"" : "")+">专科</option>");
                out.println("              <option value=\"3\" "+("3".equals(edu)? "selected=\"selected\"" : "")+">本科</option>");
                out.println("           </select><br/>");
                out.println("      爱好：<input type=\"checkbox\" name=\"hobbies\" value=\"1\" "+getChecked("1",hobbies)+"/>吃");
                out.println("           <input type=\"checkbox\" name=\"hobbies\" value=\"2\" "+getChecked("2",hobbies)+"/>玩");
                out.println("           <input type=\"checkbox\" name=\"hobbies\" value=\"3\" "+getChecked("3",hobbies)+"/>睡<br/>");
                out.println("      简介：<textarea rows=\"5\" cols=\"20\" name=\"description\">"+description+"</textarea><br/>");
                out.println("      <input type=\"submit\" value=\"修改\"/>");
                out.println("    </form>");
                out.println("");
                out.println("</body>");
                out.println("</html>");
            }

            con.commit();
        }catch (Exception e){
            e.printStackTrace();
            try {
                con.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }finally{
            JdbcUtil.close(rs,ps,con);
        }

    }

    private String getChecked(String value,String hobbies){
        if(hobbies!=null){
            String[] s=hobbies.split(",");
            for (String hobby : s) {
                if(hobby.equals(value)){
                    return "checked=\"checked\"";
                }
            }
        }
        return "";
    }













}
